package com.work.user.dao;

import com.github.pagehelper.Page;
import com.work.user.pojo.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Param;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;
import java.util.Map;

public interface UserMapper extends Mapper<User> {

    //设置用户和角色一对多关系
    @Insert("insert into t_user_role(uid,rid) values(#{uid},#{rid})")
    void setUserAndRole(Map map);

    //查询中间表，查看用户所拥有的角色id
    @Select("select rid from t_user_role where uid = #{id}")
    List<Integer> findUserIdsByRoleId(Integer id);

    //删除中间关系表
    @Delete("delete from t_user_role where uid = #{id}")
    void deleteAssocication(Integer id);

    //添加用户
    @Insert("insert into t_user(name,username,email,phone,status) value(#{name},#{username},#{email}," +
            "#{phone},#{status})")
    @Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
    void add(User user);

    //修改用户状态
    @Update("update t_user set status = #{status} where id = #{id}")
    void updateStatus(User user);

    //查询所有用户
    @Select("select id,name,username,email,phone,status from t_user")
    List<User> findAll();

    //根据id查询用户
    @Select("select * from t_user where id = #{id}")
    User findById(Integer id);

//    @Select("select password from t_user where username = #{username}")
//    String findByPassword(String username);

    //修改用户密码
    @Update("update t_user set password = #{password} where id = #{id}")
    void updatePassword(User user);
}
